﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Gmis.IDAL;
using Gmis.DBUtility;//Please add references
namespace Gmis.SqlServerDAL
{
    /// <summary>
    /// 数据访问类:Committee
    /// </summary>
    public partial class Committee : ICommittee
    {
        public Committee()
        { }
        #region  Method

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string sId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Committee");
            strSql.Append(" where sId=@sId ");
            SqlParameter[] parameters = {
					new SqlParameter("@sId", SqlDbType.VarChar,50)};
            parameters[0].Value = sId;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Gmis.Model.Committee model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Committee(");
            strSql.Append("sId,sRelationId,sPosition,sWork,sWorkPlace,dWork,dInVillage,sDuty,sTarget,sPicture,sComment,sNumber)");
            strSql.Append(" values (");
            strSql.Append("@sId,@sRelationId,@sPosition,@sWork,@sWorkPlace,@dWork,@dInVillage,@sDuty,@sTarget,@sPicture,@sComment,@sNumber)");
            SqlParameter[] parameters = {
					new SqlParameter("@sId", SqlDbType.VarChar,50),
					new SqlParameter("@sRelationId", SqlDbType.VarChar,50),
					new SqlParameter("@sPosition", SqlDbType.NVarChar,50),
					new SqlParameter("@sWork", SqlDbType.NVarChar,50),
					new SqlParameter("@sWorkPlace", SqlDbType.NVarChar,50),
					new SqlParameter("@dWork", SqlDbType.DateTime),
					new SqlParameter("@dInVillage", SqlDbType.DateTime),
					new SqlParameter("@sDuty", SqlDbType.NVarChar,128),
					new SqlParameter("@sTarget", SqlDbType.NVarChar,128),
					new SqlParameter("@sPicture", SqlDbType.NVarChar,256),
					new SqlParameter("@sComment", SqlDbType.NVarChar,300),
                    new SqlParameter("@sNumber", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.sId;
            parameters[1].Value = model.sRelationId;
            parameters[2].Value = model.sPosition;
            parameters[3].Value = model.sWork;
            parameters[4].Value = model.sWorkPlace;
            parameters[5].Value = model.dWork;
            parameters[6].Value = model.dInVillage;
            parameters[7].Value = model.sDuty;
            parameters[8].Value = model.sTarget;
            parameters[9].Value = model.sPicture;
            parameters[10].Value = model.sComment;
            parameters[11].Value = model.sNumber;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Gmis.Model.Committee model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Committee set ");
            strSql.Append("sRelationId=@sRelationId,");
            strSql.Append("sPosition=@sPosition,");
            strSql.Append("sWork=@sWork,");
            strSql.Append("sWorkPlace=@sWorkPlace,");
            strSql.Append("dWork=@dWork,");
            strSql.Append("dInVillage=@dInVillage,");
            strSql.Append("sDuty=@sDuty,");
            strSql.Append("sTarget=@sTarget,");
            strSql.Append("sPicture=@sPicture,");
            strSql.Append("sComment=@sComment,");
            strSql.Append("sNumber=@sNumber ");
            strSql.Append(" where sId=@sId ");
            SqlParameter[] parameters = {
					new SqlParameter("@sRelationId", SqlDbType.VarChar,50),
					new SqlParameter("@sPosition", SqlDbType.NVarChar,50),
					new SqlParameter("@sWork", SqlDbType.NVarChar,50),
					new SqlParameter("@sWorkPlace", SqlDbType.NVarChar,50),
					new SqlParameter("@dWork", SqlDbType.DateTime),
					new SqlParameter("@dInVillage", SqlDbType.DateTime),
					new SqlParameter("@sDuty", SqlDbType.NVarChar,128),
					new SqlParameter("@sTarget", SqlDbType.NVarChar,128),
					new SqlParameter("@sPicture", SqlDbType.NVarChar,256),
					new SqlParameter("@sComment", SqlDbType.NVarChar,300),
                    new SqlParameter("@sNumber", SqlDbType.NVarChar,50),
					new SqlParameter("@sId", SqlDbType.VarChar,50)};
            parameters[0].Value = model.sRelationId;
            parameters[1].Value = model.sPosition;
            parameters[2].Value = model.sWork;
            parameters[3].Value = model.sWorkPlace;
            parameters[4].Value = model.dWork;
            parameters[5].Value = model.dInVillage;
            parameters[6].Value = model.sDuty;
            parameters[7].Value = model.sTarget;
            parameters[8].Value = model.sPicture;
            parameters[9].Value = model.sComment;
            parameters[10].Value = model.sNumber;
            parameters[11].Value = model.sId;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string sId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Committee ");
            strSql.Append(" where sId=@sId ");
            SqlParameter[] parameters = {
					new SqlParameter("@sId", SqlDbType.VarChar,50)};
            parameters[0].Value = sId;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string sIdlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Committee ");
            strSql.Append(" where sId in (" + sIdlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Gmis.Model.Committee GetModel(string sId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 c.sId,sNumber,sRelationId,sPosition,sWork,sWorkPlace,dWork,dInVillage,sDuty,sTarget,sPicture,c.sComment,sName,sSex,sNation,sEducation,sIdentity,dBorn,nAge,sNative,sPolitics,sMarry,sMobile,sAddress,sLive ");
            strSql.Append(" from Committee as c inner join Population as p on c.sRelationId = p.sId ");
            strSql.Append(" where c.sId=@sId ");
            SqlParameter[] parameters = {
					new SqlParameter("@sId", SqlDbType.VarChar,50)};
            parameters[0].Value = sId;

            Gmis.Model.Committee model = new Gmis.Model.Committee();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["sId"] != null && ds.Tables[0].Rows[0]["sId"].ToString() != "")
                {
                    model.sId = ds.Tables[0].Rows[0]["sId"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sNumber"] != null && ds.Tables[0].Rows[0]["sNumber"].ToString() != "")
                {
                    model.sNumber = ds.Tables[0].Rows[0]["sNumber"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sRelationId"] != null && ds.Tables[0].Rows[0]["sRelationId"].ToString() != "")
                {
                    model.sRelationId = ds.Tables[0].Rows[0]["sRelationId"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sPosition"] != null && ds.Tables[0].Rows[0]["sPosition"].ToString() != "")
                {
                    model.sPosition = ds.Tables[0].Rows[0]["sPosition"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sWork"] != null && ds.Tables[0].Rows[0]["sWork"].ToString() != "")
                {
                    model.sWork = ds.Tables[0].Rows[0]["sWork"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sWorkPlace"] != null && ds.Tables[0].Rows[0]["sWorkPlace"].ToString() != "")
                {
                    model.sWorkPlace = ds.Tables[0].Rows[0]["sWorkPlace"].ToString();
                }
                if (ds.Tables[0].Rows[0]["dWork"] != null && ds.Tables[0].Rows[0]["dWork"].ToString() != "")
                {
                    model.dWork = DateTime.Parse(ds.Tables[0].Rows[0]["dWork"].ToString());
                }
                if (ds.Tables[0].Rows[0]["dInVillage"] != null && ds.Tables[0].Rows[0]["dInVillage"].ToString() != "")
                {
                    model.dInVillage = DateTime.Parse(ds.Tables[0].Rows[0]["dInVillage"].ToString());
                }
                if (ds.Tables[0].Rows[0]["sDuty"] != null && ds.Tables[0].Rows[0]["sDuty"].ToString() != "")
                {
                    model.sDuty = ds.Tables[0].Rows[0]["sDuty"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sTarget"] != null && ds.Tables[0].Rows[0]["sTarget"].ToString() != "")
                {
                    model.sTarget = ds.Tables[0].Rows[0]["sTarget"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sPicture"] != null && ds.Tables[0].Rows[0]["sPicture"].ToString() != "")
                {
                    model.sPicture = ds.Tables[0].Rows[0]["sPicture"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sComment"] != null && ds.Tables[0].Rows[0]["sComment"].ToString() != "")
                {
                    model.sComment = ds.Tables[0].Rows[0]["sComment"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sName"] != null && ds.Tables[0].Rows[0]["sName"].ToString() != "")
                {
                    model.sName = ds.Tables[0].Rows[0]["sName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sSex"] != null && ds.Tables[0].Rows[0]["sSex"].ToString() != "")
                {
                    model.sSex = ds.Tables[0].Rows[0]["sSex"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sNation"] != null && ds.Tables[0].Rows[0]["sNation"].ToString() != "")
                {
                    model.sNation = ds.Tables[0].Rows[0]["sNation"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sEducation"] != null && ds.Tables[0].Rows[0]["sEducation"].ToString() != "")
                {
                    model.sEducation = ds.Tables[0].Rows[0]["sEducation"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sIdentity"] != null && ds.Tables[0].Rows[0]["sIdentity"].ToString() != "")
                {
                    model.sIdentity = ds.Tables[0].Rows[0]["sIdentity"].ToString();
                }
                if (ds.Tables[0].Rows[0]["dBorn"] != null && ds.Tables[0].Rows[0]["dBorn"].ToString() != "")
                {
                    model.dBorn = DateTime.Parse(ds.Tables[0].Rows[0]["dBorn"].ToString());
                }
                if (ds.Tables[0].Rows[0]["nAge"] != null && ds.Tables[0].Rows[0]["nAge"].ToString() != "")
                {
                    model.nAge = int.Parse(ds.Tables[0].Rows[0]["nAge"].ToString());
                }
                if (ds.Tables[0].Rows[0]["sNative"] != null && ds.Tables[0].Rows[0]["sNative"].ToString() != "")
                {
                    model.sNative = ds.Tables[0].Rows[0]["sNative"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sPolitics"] != null && ds.Tables[0].Rows[0]["sPolitics"].ToString() != "")
                {
                    model.sPolitics = ds.Tables[0].Rows[0]["sPolitics"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sMarry"] != null && ds.Tables[0].Rows[0]["sMarry"].ToString() != "")
                {
                    model.sMarry = ds.Tables[0].Rows[0]["sMarry"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sMobile"] != null && ds.Tables[0].Rows[0]["sMobile"].ToString() != "")
                {
                    model.sMobile = ds.Tables[0].Rows[0]["sMobile"].ToString();
                }
                if (ds.Tables[0].Rows[0]["sAddress"] != null && ds.Tables[0].Rows[0]["sAddress"].ToString() != "")
                {
                    model.sAddress = ds.Tables[0].Rows[0]["sAddress"].ToString();
                }               
                if (ds.Tables[0].Rows[0]["sLive"] != null && ds.Tables[0].Rows[0]["sLive"].ToString() != "")
                {
                    model.sLive = ds.Tables[0].Rows[0]["sLive"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select c.sId,sNumber,sRelationId,sPosition,sWork,sWorkPlace,dWork,dInVillage,sDuty,sTarget,sPicture,c.sComment,sName,sSex,sNation,sEducation,sIdentity,dBorn,nAge,sNative,sPolitics,sMarry,sMobile,sAddress,sLive ");
            strSql.Append(" FROM Committee as c inner join Population as p on c.sRelationId = p.sId ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" c.sId,sNumber,sRelationId,sPosition,sWork,sWorkPlace,dWork,dInVillage,sDuty,sTarget,sPicture,c.sComment,sName,sSex,sNation,sEducation,sIdentity,dBorn,nAge,sNative,sPolitics,sMarry,sMobile,sAddress,sLive ");
            strSql.Append(" FROM Committee as c inner join Population as p on c.sRelationId = p.sId ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "Committee";
            parameters[1].Value = "sId";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

